<template>
  <div class="login">
    <div class="body">
      <div class="top">
        <img src="../assets/image/login1.png" alt="" />
        <span>欢迎来到爱课！</span>
      </div>
      <van-form @submit="onSubmit">
        <van-cell-group inset>
          <van-field
            v-model="phone"
            name="name"
            placeholder="请输入手机号"
            :rules="[
              {
                required: true,
                pattern: pattern,
                message: '请输入正确的手机号',
              },
            ]"
          />
          <van-field v-model="code" name="code" placeholder="请输入验证码" />

          <van-button type="primary" @click="getCode(phone)" class="code" round :disabled="istrue">
            获取验证码
            <span v-if="istrue">({{ num }})s</span>
          </van-button>
        </van-cell-group>
        <div style="margin: 16px">
          <van-button round block type="primary" native-type="submit" class="submit">
            登录/注册
          </van-button>
        </div>
      </van-form>
    </div>
    <div class="btm">
      <img src="../assets/image/login2.png" alt="" />
      <p>
        登录即代表阅读并同意爱课
        <span>《服务使用协议》&nbsp;&nbsp;《隐私政策》</span>
      </p>
    </div>
  </div>
</template>

<script>
import { loginApi, getCodeApi } from "../utils/api.js";
import { ref } from "vue";
import { useRouter } from "vue-router";
import { Toast } from "vant";
export default {
  setup() {
    const router = useRouter();
    const istrue = ref(false);
    const num = ref(0);
    const phone = ref("");
    const code = ref("");
    const pattern =
      /^(?:(?:\+|00)86)?1(?:(?:3[\d])|(?:4[5-79])|(?:5[0-35-9])|(?:6[5-7])|(?:7[0-8])|(?:8[\d])|(?:9[189]))\d{8}$/;
    const onSubmit = async values => {
      console.log("submit", values);
      let res = await loginApi({
        phone: phone.value + "",
        code: code.value + "",
      });

      if (res.status === 0) {
        localStorage.setItem("token", res.token);
        router.push("/home");
      } else {
        Toast.fail("手机号或验证码错误");
      }
    };
    const getCode = async value => {
      if (value) {
        num.value = 10;
        istrue.value = true;
        let timer = setInterval(() => {
          num.value--;
          if (num.value == 0) {
            istrue.value = false;
            clearInterval(timer);
          }
        }, 1000);
        let res = await getCodeApi({ phone: value });
        console.log(res.code);
      } else {
        Toast.fail("请输入手机号");
      }
    };
    return {
      phone,
      code,
      pattern,
      onSubmit,
      getCode,
      istrue,
      num,
    };
  },
};
</script>

<style lang="less" scoped>
.login {
  background-color: #fff;
  .body {
    padding: 100px 38px 200px;
    .top {
      height: 40px;
      display: flex;
      margin-bottom: 40px;
      span {
        line-height: 40px;
        font-size: 18px;
      }
      img {
        width: 40px;
        height: 40px;
        margin-right: 4px;
      }
    }
  }
  .btm {
    position: fixed;
    bottom: 0;
    p {
      position: fixed;
      bottom: 14px;
      font-size: 11px;
      left: 36px;
      color: #737777;
      span {
        color: #19220e;
      }
    }
  }
}

.van-form {
  width: 100%;
  position: relative;
  .code {
    position: absolute;
    right: 20px;
    bottom: 15px;
    height: 40px;
  }
  .van-cell-group {
    margin-right: 0px;
    margin-left: 0px;
    position: relative;
  }
  /deep/.van-field__body {
    width: 100%;
    height: 50px;
    border-radius: 50px;
    background-color: #f5f5f5;
    input {
      width: 60%;
      margin-left: 20px;
    }
    input::-webkit-input-placeholder {
      position: relative;
      left: 20px;
    }
  }
}
.submit {
  margin-top: 40px;
}
</style>
